package logica_de_presentacion;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import comun.Imagen;

public class VerImagen extends JInternalFrame implements ActionListener {

	/**
	 * Ver Imagen
	 */

	private static final long serialVersionUID = 1L;
	private JButton Aceptar,Cancelar;
	private boolean booleanAdd = false;

	public VerImagen() {

		super("Imagen",true,true,true,true);
		JPanel jpBotones=new JPanel();
		this.getContentPane().setLayout(new BorderLayout());

		Aceptar = new JButton("Aceptar");
		Aceptar.setMnemonic(KeyEvent.VK_A);
		Aceptar.addActionListener(this);
		jpBotones.add(Aceptar);
		
		Cancelar = new JButton("Cancelar");
		Cancelar.setMnemonic(KeyEvent.VK_C);
		Cancelar.addActionListener(this);

		this.getContentPane().setLayout(new BorderLayout());
		jpBotones.add(Cancelar);
		this.getContentPane().add(jpBotones, BorderLayout.SOUTH);
		this.setVisible(true);
		this.setSize(1300,800);

	}

	public void cargarImagen(String path) throws IOException {

		JLabel hasta = new JLabel();
		ImageIcon tick = readImageFromFile(path);
		hasta.setIcon(tick);
		this.getContentPane().add(hasta, BorderLayout.CENTER);
	}
	
	public void cargarImagen(Imagen i) throws IOException {

		JLabel hasta = new JLabel();
		hasta.setIcon(i.getImg());
		this.getContentPane().add(hasta, BorderLayout.CENTER);
	}

	public ImageIcon readImageFromFile(String filePath) throws IOException {
		BufferedImage image = ImageIO.read(new File(filePath));
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		ImageIO.write(image, "jpg", baos);
		ImageIcon i=new ImageIcon(baos.toByteArray());
		return i;
	}
	/** Returns an ImageIcon, or null if the path was invalid. */
	protected static ImageIcon createImageIcon(String path) {
		java.net.URL imgURL = Button.class.getResource(path);
		if (imgURL != null) {
			return new ImageIcon(imgURL);
		} else {
			System.err.println("Couldn't find file: " + path);
			return null;
		}
	}
	@Override
	public void actionPerformed(ActionEvent e) {

		if ((JButton) e.getSource() == Aceptar) {
			booleanAdd = true;
			this.dispose();
		}else{
			this.dispose();
		}
	}

	public boolean getAdd() {
		return booleanAdd;
	}
}